package kr.slm.updatetool.view;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import kr.slm.updatetool.paser.XPathTest;

import org.eclipse.e4.ui.workbench.IWorkbench;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class ViewPart {
	private Text now_path_txt;
	private Text lastest_path_txt;

	private final String defalut_path = "c:\\";

	@Inject
	IWorkbench workbench;

	@PostConstruct
	public void postConstruct(Composite parent) throws Exception {
		parent.setLayout(null);

		final ProgressBar progressBar = new ProgressBar(parent, SWT.NONE);
		progressBar.setBounds(0, 281, 448, 17);

		Button btnNewButton = new Button(parent, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@SuppressWarnings("static-access")
			@Override
			public void widgetSelected(SelectionEvent e) {

				XPathTest pathTest = new XPathTest();
				try {
					pathTest.start(progressBar, lastest_path_txt.getText(), now_path_txt.getText());

				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} finally {
					//workbench.restart(); 재시작

				}

			}
		});
		btnNewButton.setBounds(342, 65, 76, 44);
		btnNewButton.setText("업데이트");

		now_path_txt = new Text(parent, SWT.BORDER);
		now_path_txt.setBounds(76, 58, 229, 17);

		lastest_path_txt = new Text(parent, SWT.BORDER);
		lastest_path_txt.setBounds(76, 103, 229, 17);

		Label label = new Label(parent, SWT.NONE);
		label.setBounds(7, 57, 52, 18);
		label.setText("현재 폴더");

		Label label_1 = new Label(parent, SWT.NONE);
		label_1.setText("최신 폴더");
		label_1.setBounds(7, 102, 52, 18);

		Button btnNewButton_1 = new Button(parent, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(e.display.getActiveShell());
				dialog.setFilterPath(defalut_path + "test"); // 기본경로설정
				dialog.setText("Open");
				String path = dialog.open();
				if (path == null)
					return;
				now_path_txt.setText(path);

			}
		});
		btnNewButton_1.setBounds(312, 58, 24, 17);
		btnNewButton_1.setText("...");

		Button button = new Button(parent, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				DirectoryDialog dialog = new DirectoryDialog(e.display.getActiveShell());
				dialog.setFilterPath(defalut_path + "test2"); //기본경로설정
				dialog.setText("Open");
				String path = dialog.open();
				if (path == null)
					return;
				lastest_path_txt.setText(path);
			}
		});
		button.setText("...");
		button.setBounds(312, 103, 24, 17);

		Label lblSlmSampleUpdate = new Label(parent, SWT.NONE);
		lblSlmSampleUpdate.setFont(SWTResourceManager.getFont("맑은 고딕", 23, SWT.BOLD));
		lblSlmSampleUpdate.setBounds(0, 0, 408, 49);
		lblSlmSampleUpdate.setText("SLM Sample Update Tool");
	}
}